package wowrl.gui.CharacterCreation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import wowrl.data.Player;
import wowrl.engine.WOWRL_Engine;

public class CreateAttributes  extends JPanel{

	WOWRL_Engine engine;
	static Color backgroundColor;

	private JButton doneButton;
	
	private JLabel attributePool;
	private JLabel attributeHeader;

	private JButton[] attributeUp;
	private JButton[] attributeDown;
	private JLabel[] attributeDisplay;
	private JLabel[] attributeName;
	private JLabel[] attributeSummary;

	
	private int defaultAttribute = 10;
	private int minAttribute = 8;
	private int maxAttribute = 18;
	private int attributePoints = 16;
	
	public boolean done = false;
	
	public CreateAttributes(WOWRL_Engine engine)
	{
		this.engine = engine;
		
		//Declare the arrays
		attributeUp = new JButton[6];
		attributeDown = new JButton[6];
		attributeDisplay = new JLabel[6];
		attributeName = new JLabel[6];
		attributeSummary = new JLabel[6];
		
		// Specify the background color
		backgroundColor = new Color((float) 0.3, (float) 0.65, (float) 0.3);

		// We will manually layout
		setLayout(null);
		
		//Create the done button
		doneButton = new JButton(new AbstractAction("DoneButton")
		{
			public void actionPerformed(ActionEvent e)
			{
				endCreation();
			}			
		});
		doneButton.setText("DONE");
		
		// attributePool
		attributePool = new JLabel("INIT ERROR", JLabel.CENTER);
		attributePool.setFont(new Font("Serif", Font.BOLD, 18));
		attributePool.setForeground(Color.red);
		attributePool.setOpaque(true);		
		add(attributePool);
		
		// attributeHeader
		attributeHeader = new JLabel("ATTRIBUTES", JLabel.CENTER);
		attributeHeader.setFont(new Font("Serif", Font.BOLD, 24));
		attributeHeader.setForeground(Color.black);
		attributeHeader.setOpaque(true);	
		add(attributeHeader);
		
		//Create the attribute buttons
		int buttonYStart = 45;
		int buttonYSize = 25;
		int buttonYSpacing = buttonYSize+5;
		
		int buttonXStart = 80;
		int buttonXSize = 45;
		int buttonXSpacing = buttonXSize+5;
		
		int displayXSize = 60;
		int displayXSpacing = displayXSize+5;
		
		for (int attribute=0;attribute<6;attribute++)
		{
			//Populate the default value
			engine.getPlayer().attributes[attribute] = defaultAttribute;
			
			//Create the buttons
			final int thisAttribute = attribute;			
			attributeDown[attribute] = new JButton(new AbstractAction("LowerAttribute" + attribute)
			{
				public void actionPerformed(ActionEvent e)
				{
					lowerAttribute(thisAttribute);
				}			
			});
			attributeUp[attribute] = new JButton(new AbstractAction("RaiseAttribute" + attribute)
			{
				
				public void actionPerformed(ActionEvent e)
				{
					raiseAttribute(thisAttribute);
				}			
			});
			
			//Create the labels
			attributeDisplay[attribute] = new JLabel("--",JLabel.CENTER);
			attributeDisplay[attribute].setFont(new Font("Serif", Font.BOLD, 18));
			attributeDisplay[attribute].setForeground(Color.black);
			attributeDisplay[attribute].setOpaque(true);
			
			attributeName[attribute] = new JLabel(Player.attributeNames[attribute],JLabel.CENTER);
			attributeName[attribute].setFont(new Font("Serif", Font.BOLD, 18));
			attributeName[attribute].setForeground(Color.black);
			attributeName[attribute].setOpaque(true);
			
			attributeSummary[attribute] = new JLabel(Player.attributeSummaries[attribute],JLabel.LEFT);
			attributeSummary[attribute].setFont(new Font("Serif", Font.BOLD, 18));
			attributeSummary[attribute].setForeground(Color.black);
			attributeSummary[attribute].setOpaque(true);
			
			//Set button labels
			attributeDown[attribute].setText("-");
			attributeUp[attribute].setText("+");
			attributeDisplay[attribute].setText(Integer.toString(10));
			
			//Set positions
			attributeName[attribute].setBounds(10, buttonYStart + (buttonYSpacing*attribute), 60, buttonYSize);
			attributeDown[attribute].setBounds(buttonXStart, buttonYStart + (buttonYSpacing*attribute), buttonXSize, buttonYSize);
			attributeDisplay[attribute].setBounds(buttonXStart+buttonXSpacing,buttonYStart + (buttonYSpacing*attribute),displayXSize, buttonYSize);
			attributeUp[attribute].setBounds(buttonXStart+buttonXSpacing+displayXSpacing, buttonYStart + (buttonYSpacing*attribute), buttonXSize, buttonYSize);
			attributeSummary[attribute].setBounds(buttonXStart+(buttonXSpacing*2)+displayXSpacing+20, buttonYStart + (buttonYSpacing*attribute), 400, buttonYSize);
			
			//Add to JPanel
			add(attributeName[attribute]);
			add(attributeDown[attribute]);
			add(attributeDisplay[attribute]);
			add(attributeUp[attribute]);
			add(attributeSummary[attribute]);
		}
		
		doneButton.setLocation(20,20);
		doneButton.setSize(50,20);
		add(doneButton);
		
		//Layout the display (X,Y,width,height)
		attributeHeader.setBounds(10,10,170,buttonYSize+5);
		attributePool.setBounds    (185, 13, 170, buttonYSize);
		doneButton.setBounds   (20,buttonYStart + (buttonYSpacing*7), 90, 25);
		
		// Set the window size
		setPreferredSize(new Dimension(670, buttonYStart + (buttonYSpacing*8)));
		
		refreshDisplay();
	}
	
	private void refreshDisplay()
	{
		for (int attribute=0;attribute<6;attribute++)
		{
			attributeDisplay[attribute].setText("" + engine.getPlayer().attributes[attribute]);
		}
		
		attributePool.setText("Points left: " + attributePoints);
	}
	
	public void raiseAttribute(int attribute)
	{
		if (attributePoints>0 && engine.getPlayer().attributes[attribute] < maxAttribute)
		{
			engine.getPlayer().attributes[attribute]++;
			attributePoints--;
		}
		refreshDisplay();
	}
	
	public void lowerAttribute(int attribute)
	{		
		if (engine.getPlayer().attributes[attribute] > minAttribute)
		{
			engine.getPlayer().attributes[attribute]--;
			attributePoints++;
		}
		refreshDisplay();
	}
	
	public void endCreation()
	{
		done=true;
	}
}
